﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
using System.Timers;
using TeamDev.Redis;
using Redis.Utils;
using Web.Helper;
using Entity;
namespace MvcApplication1.Controllers
{
    /// <summary>
    /// 消息对象类型
    /// </summary>
    public class MessageQuene
    {
        static System.Timers.Timer timer = new System.Timers.Timer(5000);
        public static ChatModels CurrentChatModels = new ChatModels();
        static Redis.Utils.RedisClient redisClient;
        static MessageQuene()
        {
            redisClient = new Redis.Utils.RedisClient();
            //redisClient = RedisClientFactory.GetInstance();
            timer.AutoReset = true;
            timer.Enabled = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);//subscribe a event
            timer.Start();

        }
        private static void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            CurrentChatModels = (ChatModels)redisClient.LeftPop("MessageQuene");
        }
    }
    [HandleError]
    public class HomeController : Controller
    {
        Redis.Utils.RedisClient redisClient;

        public HomeController()
        {
            redisClient = new Redis.Utils.RedisClient();
        }

        public ActionResult Index()
        {

            ViewData["pop"] = MessageQuene.CurrentChatModels == null
                ? "没有记录"
                : MessageQuene.CurrentChatModels.Chat;
            ViewData["MSMQ"] = redisClient.ListRange("MessageQuene") == null
              ? new List<ChatModels>()
              : redisClient.ListRange("MessageQuene").Cast<ChatModels>().ToList();
            ViewData["ChatList"] = redisClient.ListRange("Chat") == null
                ? new List<ChatModels>()
                : redisClient.ListRange("Chat").Cast<ChatModels>().ToList();



            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            #region KV存储
            redisClient.RightPush("Chat", new ChatModels { UserID = form["UserID"], Chat = form["Chat"] });
            redisClient.BackgroundSave();
            int start, end;
            if (!int.TryParse(form["Start"], out start)) start = 0;
            if (!int.TryParse(form["End"], out end)) end = 20;
            ViewData["ChatList"] = redisClient.ListRange("Chat") == null
                ? new List<ChatModels>()
                : redisClient.ListRange("Chat", start, end).Cast<ChatModels>().ToList();
            #endregion

            #region MSMQ消息队列服务
            redisClient.RightPush("MessageQuene", new ChatModels { UserID = form["UserID"], Chat = form["Chat"] });
            redisClient.BackgroundSave();
            ViewData["MSMQ"] = redisClient.ListRange("MessageQuene") == null
                 ? new List<ChatModels>()
                : redisClient.ListRange("MessageQuene", start, end).Cast<ChatModels>().ToList();
            #endregion

            return View();
        }
        public ActionResult About()
        {


            RedisClient_SortedSet rs = new RedisClient_SortedSet();



            rs.Add("rs1", 1, "one");
            rs.Add("rs1", 2, "two");
            rs.Add("rs1", 3, "three");
            rs.AddScore("rs1", -10, "three");

            List<object> list = new List<object>();
            ViewData.Add("roleset", list); //set getValue
            ViewData["rank"] = rs.Rank("rs1", "one");
            return View();
        }

        [HttpPost]
        public ActionResult About(FormCollection form)
        {
            redisClient.LeftPop("RoleName");
            ViewData["roleName"] = redisClient.ListRange("RoleName").Cast<string>().ToList();

            return View();
        }

        public ActionResult TestRedis(int? cid, int? vid, int? page)
        {

            var model = new Data.Redis.Res_ItemRepository().GetRes_Item(cid, vid, page ?? 1);
            if (Request.IsAjaxRequest())
                return PartialView("RedisList", model);
            return View(model);
        }
    }

}
